<?php
/// ------------
/// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
/// ------------
/// | Copyright (c) 2006-2012 http:///thinkphp.cn All rights reserved.
/// ------------
/// | Licensed ( http:///www.apache.org/licenses/LICENSE-2.0 )
/// ------------
/// | Author: liu21st <liu21st@gmail.com>
/// ------------
/// $Id: ContentReplaceBehavior.class.php 2929 2012-05-02 06:45:47Z liu21st@gmail.com $

!defined('THINK_PATH') && exit();
/**
 * 系统行为扩展 模板内容输出替换
 */
class ContentReplaceBehavior extends Behavior {
    /// 行为参数定义
    protected $options   =  array(
        'TMPL_PARSE_STRING'=>array(),
    );

    /// 行为扩展的执行入口必须是run
    public function run(&$content){
        $content = $this->templateContentReplace($content);
    }

    /**
     * 模板内容替换
     * @access protected
     * @param string $content 模板内容
     * @return string
     */
    protected function templateContentReplace($content) {  
    	$var_page	=	$_REQUEST[C('VAR_PAGE')]?$_REQUEST[C('VAR_PAGE')]:1;
        /// 系统默认的特殊变量替换
        $search_start = '<div class="search_box">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td>
    <div class="search_left">';
        $search_end = '</div>
    </td>
    <td width="68" height="40" background="'.__ROOT__.'/DzhPHP/Public/Images/Default/search_button_bg.gif" bgcolor="#DBEAFF" align="center">
    <input type="hidden" name="search_form" value="1">
    <input type="hidden" name="'.C('VAR_PAGE').'" id="'.C('VAR_PAGE').'" value="'.$var_page.'">
    <button class="mout_search" value="1" onmouseout="this.className=\'mout_search\'" onmouseover="this.className=\'mover_search\'" name="ac_search" id="ac_search" type="submit">'.L('search').'</button>
    </td>
  </tr>
</table>
</div>';
        if(!defined('FrameName')){
        		$framename='DzhPHP';
      	}else{
      			$framename=FrameName;
      				 }
        $replace =  array(
            '__TMPL__'      => APP_TMPL_PATH,  /// 项目模板目录
            '__ROOT__'      => __ROOT__,       /// 当前网站地址
            '__APP_ROOT__'  => __APP_ROOT__,   /// 当前网站根地址
            '__APP__'       => __APP__,        /// 当前项目地址
            '__GROUP__'   =>   defined('GROUP_NAME')?__GROUP__:__APP__,
            '__ACTION__'    => __ACTION__,     /// 当前操作地址
            '__SELF__'      => __SELF__,       /// 当前页面地址
            '__URL__'       => __URL__,
            '../Public'   => __APP_ROOT__.'/Public',/// 项目公共模板目录
            '__PUBLIC__'  => __ROOT__.'/'.$framename.'/Public',/// 站点公共目录
            '__SEARCH_START__'  => $search_start,
            '__SEARCH_END__'  => $search_end,/// 站点公共目录
            '__*__' 	=> '<span class="font_red">*</span>',
        );
        /// 允许用户自定义模板的字符串替换
        if(is_array(C('TMPL_PARSE_STRING')) )
            $replace =  array_merge($replace,C('TMPL_PARSE_STRING'));
        $content = str_replace(array_keys($replace),array_values($replace),$content);
        return $content;
    }

}